package com.bac.rentmap.web.controller.ad.search;

import java.io.IOException;
import java.util.logging.Logger;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;

import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;

public class CachingFlatAdSearchController extends FlatAdSearchController {
    private static String LOGGED_IN_PARAMETER_NAME = "loggedIn";
    private static String LOGGED_IN_MARKER = LOGGED_IN_PARAMETER_NAME + "=true";

    private static final Logger log = Logger
    .getLogger(CachingFlatAdSearchController.class.getName());

    @Override
    protected ModelAndView handleRequestInternal(HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        UserService userService = UserServiceFactory.getUserService();
        boolean loggedIn = userService.isUserLoggedIn();

        if (loggedIn) {
            if(request.getParameter(LOGGED_IN_PARAMETER_NAME) == null){
                addLoginInfoToUrl(request, response);
                return null;
            }
            response.addHeader("Cache-Control", "private");
        }
        else{
            if(request.getParameter(LOGGED_IN_PARAMETER_NAME) != null){
                removeLoginInfoFromUrl(request, response);
                return null;
            }
            response.addHeader("Cache-Control", "public");
        }
        return super.handleRequestInternal(request, response);
    }

    private void addLoginInfoToUrl(HttpServletRequest request, HttpServletResponse response) throws IOException{
        String queryString = request.getQueryString();
        log.info("Adding login info to query string: " + queryString);
        if(queryString == null){
            queryString = "";
        }
        if(!queryString.isEmpty()){
            queryString += "&";
        }
        queryString += LOGGED_IN_MARKER;

        performRedirect(request, response, queryString);
    }

    private void performRedirect(HttpServletRequest request, HttpServletResponse response, String queryString) throws IOException{
        String url;
        String pageString = request.getParameter("page");
        if(pageString != null){
            url = request.getRequestURI();
        }
        else{
            url = "/";
        }
        if(queryString != null && !queryString.isEmpty()){
            url += "?" + queryString;
        }
        response.sendRedirect(response.encodeRedirectURL(url));
    }

    private void removeLoginInfoFromUrl(HttpServletRequest request, HttpServletResponse response) throws IOException{
        String queryString = request.getQueryString();
        log.info("Removing login info from query string: " + queryString);
        queryString = queryString.replaceAll("&" + LOGGED_IN_MARKER, "");
        queryString = queryString.replaceAll(LOGGED_IN_MARKER, "");
        performRedirect(request, response, queryString);
    }
}
